package com.lemon.smartsafetynew.ui.fragment.hidden;

import android.os.Bundle;
import android.util.Log;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.view.list.MultiStateView;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenDownloadVo;
import com.lemon.smartsafetynew.net.services.SafeHiddenServices;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenCreateHiddenActivity;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenInfoActivity;
import com.lemon.smartsafetynew.ui.activity.hidden.SafeHiddenUploadHiddenActivity;
import com.lemon.smartsafetynew.ui.adapter.hidden.SafeHiddenListAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.lemon.smartsafetynew.util.user.UserTypeUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * 隐患排查列表
 */
public class SafeHiddenListFragmentNew extends BasePullToRefreshFragment<SafeHiddenDownloadVo, List<SafeHiddenDownloadVo>>
        implements RecyclerTClickListener<SafeHiddenDownloadVo> {

    // ============== 参数 ====================
    private String companyId = "";
    private String gridId = "";
    private String status = "";
    private String source = "";
    private String startTime = null;
    private String endTime = null;

    /**
     * @param status    隐患状态
     * @param source    隐患来源
     * @param startTime 起始时间
     * @param endTime   结束时间
     * @param companyId 企业ID
     * @param gridId    网格ID
     * @return
     */
    public static SafeHiddenListFragmentNew newInstance(String status, String source,
                                                        String startTime, String endTime,
                                                        String companyId, String gridId) {

        Bundle args = new Bundle();
        args.putString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_STATUS, status);
        args.putString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_SOURCE, source);
        if (startTime != null && endTime != null) {
            args.putString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_START_TIME, startTime);
            args.putString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_END_TIME, endTime);
        }
        args.putString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_COMPANY_ID, companyId);
        args.putString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_GRID_ID, gridId);

        SafeHiddenListFragmentNew fragment = new SafeHiddenListFragmentNew();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();

        if (bundle != null) {

            if (bundle.containsKey(KeyConstants.KEY_SAFE_HIDDEN_DANGER_STATUS)) {
                status = bundle.getString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_STATUS);
            }

            if (bundle.containsKey(KeyConstants.KEY_SAFE_HIDDEN_DANGER_SOURCE)) {
                source = bundle.getString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_SOURCE);
            }

            if (bundle.containsKey(KeyConstants.KEY_SAFE_HIDDEN_DANGER_START_TIME)) {
                startTime = bundle.getString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_START_TIME);
            }

            if (bundle.containsKey(KeyConstants.KEY_SAFE_HIDDEN_DANGER_END_TIME)) {
                endTime = bundle.getString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_END_TIME);
            }

            if (bundle.containsKey(KeyConstants.KEY_SAFE_HIDDEN_DANGER_COMPANY_ID)) {
                companyId = bundle.getString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_COMPANY_ID);
            }

            if (bundle.containsKey(KeyConstants.KEY_SAFE_HIDDEN_DANGER_GRID_ID)) {
                gridId = bundle.getString(KeyConstants.KEY_SAFE_HIDDEN_DANGER_GRID_ID);
            }
        }
    }

    @Override
    public void onPullRefresh() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("status", status);
        params.put("source_type", source);

        if (startTime != null && endTime != null) {
            //当选择了时间段，才传值
            params.put("start_time", String.valueOf(startTime));
            params.put("end_time", String.valueOf(endTime));
        }
        if (UserTypeUtil.isGovType(AccountUtil.getUserType())) {
            //政府人员
            params.put("company_id", companyId);
            params.put("grid_id", gridId);
        } else {
            params.put("company_id", AccountUtil.getCompanyId()); //企业人员
        }
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        SafeHiddenServices
                .init()
                .getSafeHiddenListNew(RefreshType.REFRESH, params);
    }

    @Override
    public void onLoadMore() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("status", status);
        params.put("source_type", source);

        if (startTime != null && endTime != null) {
            //当选择了时间段，才传值
            params.put("start_time", String.valueOf(startTime));
            params.put("end_time", String.valueOf(endTime));
        }

        if (UserTypeUtil.isGovType(AccountUtil.getUserType())) {
            //政府人员
            params.put("company_id", companyId);
            params.put("grid_id", gridId);
        } else {
            //企业人员
            params.put("company_id", AccountUtil.getCompanyId());
        }
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        SafeHiddenServices
                .init()
                .getSafeHiddenListNew(RefreshType.LOAD_MORE, params);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<SafeHiddenDownloadVo> getLemonAdapter() {
        return new SafeHiddenListAdapter(mDataList, this, status);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_SAFE_HIDDEN_LIST_NEW;
    }

    @Override
    public void onItemClick(SafeHiddenDownloadVo data, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO, data);
        UIHelper.showActivity(getmContext(), bundle, SafeHiddenInfoActivity.class);
    }

}
